﻿
#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H

#include <QMainWindow>
#include <QWidget>
#include <QListWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QProgressBar>
#include <QGridLayout>
#include <QString>
#include <QTableWidget>
#include <QHeaderView>
#include <QList>
#include <QTimer>

#include <QtNetwork/QHostAddress>
#include <QtNetwork/QTcpSocket>

#include "p2p_download.h"
#include "p2p_server.h"


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    enum MsgKind{
        NAMEMSG = 0,//登录的消息
        METAMSG = 1,//上传共享文件的元数据消息
        DISCONNECT = 2,//断开连接的消息
        SHAREMSG = 3,//获取我的共享文件的消息
        DELETEMSG = 4,//删除共享文件的消息
        SEARCHMSG = 5,//搜索文件的消息
        SIGNMSG = 6,//注册的消息
        OTHERMSG = 7,//其他类型的消息
        BREAKPOINT=8, //获取文件断点的信息
        UPDATELOG=9//更新下载表的记录信息
    };

    void updatelog();//打开界面，自动更新数据库的下载日志

private:
    QWidget *myWidget;

    QLabel *logLabel;
    QListWidget *ContentListWidget;

    QLabel *UserNameLabel;
    QLineEdit *UserNameLineEdit;

    QLabel *UserPasswdLabel;
    QLineEdit *UserPasswdLineEdit;

    QLabel *ServerIPLabel;
    QLineEdit *ServerIPLineEdit;

    QLabel *PortLabel;
    QLineEdit *PortLineEdit;

    QPushButton *EnterBtn;
    QPushButton *SignBtn;
    QPushButton *quitBtn;

    QTableWidget *ResultTableWidget;
    QLineEdit *SearchLineEdit;
    QPushButton *SearchBtn;
    QPushButton *StopBtn;
    QProgressBar *ProgressBar;
    QPushButton *DownloadBtn;
    QTimer *timer;

    QTableWidget *ShareTableWidget;
    QPushButton *FlushBtn;
    QPushButton *DisableBtn;
    QPushButton *BreakPointBtn;
    QPushButton *ShareBtn;

    QLineEdit *DirLineEdit;
    QPushButton *SelectDirBtn;

    QGridLayout *layout1;
    QGridLayout *layout2;
    QGridLayout *layout3;
    QGridLayout *mainLayout;
    QPushButton *PreviewBtn;


    bool status;
    int port;
    QHostAddress *ServerIP;
    QString UserName;
    QString breakpoint;//存储文件断点的分块位置
    QTcpSocket *tcpSocket;
    P2P_Server *p2pserver;
    QList<P2P_Download*> tasks;//存储下载任务，一个一个进行下载

    QString ClientDir;

    QThread thread;

    void FormatMsg(QString &msg, MsgKind kind);
    void UpdateShareTableWidget(QString &msg);
    void UpdateResultTableWidget(QString &msg);
    void UpdateBreakTableWidget(QString &msg);
    void GetFileLog();
    void previewImage(QString);
    void previewText(QString);
    void previewVideo(QString);
public slots:
    void slotEnter(QString serverIp, QString username, QString password);
    void slotSendMeta();
    void slotConnected();
    void slotDisconnected();
    void DataReceived();
    void slotFlushShare();
    void slotDeleteShare();
    void slotSearch();
    void slotSelectDir();
    void slotDownload();
    void slotBreakPoint();
    void slotStopDownload();

    void slotSignUp();
    void SignConnected();
    void SignDisconnected();
    void SignDataReceived();

    void timeout();
    void previewAll();
    void store(QString serverIp, QString username, QString password);

signals:
    void StopDownLoad();//点击中断按钮发送中断下载的信号
    void startDownload(QString dirname);
    void close();
};

#endif // TCP_CLIENT_H
